package com.gimi.cloud.bbp.manage.controller.mall;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.enums.ServiceNameConstants;
import com.gimi.cloud.bbp.common.utils.reqhead.Response;
import com.gimi.cloud.bbp.dto.mall.MallAppSpecialDTO;
import com.gimi.cloud.bbp.manage.service.mall.MallAppSpecialGroupService;
import com.gimi.cloud.bbp.manage.service.mall.MallAppSpecialService;
import com.github.pagehelper.PageInfo;

/**
 * 专场相关controller
 * @author chendi
 * @date 2018年1月31日
 */

@RestController
@RequestMapping("/mall/special")
public class MallSpecialController {
    private static final Logger logger = LoggerFactory.getLogger(MallSpecialController.class);

    private MallAppSpecialGroupService mallAppSpecialGroupService;
    private MallAppSpecialService mallAppSpecialService;

    @Autowired
    public MallSpecialController(MallAppSpecialGroupService mallAppSpecialGroupService, MallAppSpecialService mallAppSpecialService) {
        this.mallAppSpecialGroupService = mallAppSpecialGroupService;
        this.mallAppSpecialService = mallAppSpecialService;
    }

    /**
     * 获取专场列表
     * @param request
     * @param response
     * @param dto
     * @throws Exception
     */
    @RequestMapping("/list")
    public Response list(HttpServletRequest request, HttpServletResponse response,@RequestBody MallAppSpecialDTO dto) throws Exception{
        Response resp = new Response();
        List<MallAppSpecialDTO> specialDTO = mallAppSpecialService.queryList(dto,true);
        PageInfo<MallAppSpecialDTO> pageInfo = new PageInfo<>(specialDTO);
        Map<String,Object> resData = new HashMap<>();
        resData.put(ServiceNameConstants.PAGEDATA_KEY,pageInfo);
        resp.setRes_data(resData);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        return resp;
    }

    /**
     * 获取单一的专场信息
     * @param request
     * @param response
     * @param dto
     * @throws Exception
     */
    @RequestMapping("/info")
    public Response info(HttpServletRequest request,HttpServletResponse response,@RequestBody MallAppSpecialDTO dto) throws Exception{
        Response resp = new Response();
        MallAppSpecialDTO appSpecial = mallAppSpecialService.load(dto,dto.getAslId());
        Map<String,Object> resData = new HashMap<>();
        resData.put("appSpecial",appSpecial);
        resp.setRes_data(resData);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"获取成功");
        return resp;
    }

    /**
     * 新增或编辑appSpecial信息
     * @param request
     * @param response
     * @param dto 待保存的数据
     * @throws Exception
     */
    @RequestMapping("/saveOrUpdate")
    public Response saveOrUpdate(HttpServletRequest request, HttpServletResponse response,@RequestBody MallAppSpecialDTO dto) throws Exception{
        Response resp = new Response();
        mallAppSpecialService.saveOrUpdate(dto);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"更新成功");
        return resp;
    }
    /**
     * 删除appSpecial数据
     * @param request
     * @param response
     * @param dto 待删除的appSpecial数据
     * @throws Exception
     */
    @RequestMapping("/delete")
    private Response delete(HttpServletRequest request,HttpServletResponse response,@RequestBody MallAppSpecialDTO dto) throws  Exception{
        Response resp = new Response();
        mallAppSpecialService.delete(dto,dto.getAslId(),false);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"删除成功");
        return resp;
    }
}
